﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
Copyright 2010 Mirosław M. Mączka

This file is part of Flac2Wma.

    Flac2Wma is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace AddResolution
{
    // FLV "Sorenson H263"
    public class MI_connector
    {
        bool is64 = false;
        //
        MediaInfoLib.MediaInfo32 mi32 = null;
        MediaInfoLib.MediaInfo64 mi64 = null;
        //
        public MI_connector()
        {
            is64 = 8 == IntPtr.Size;
            if(is64)
            {
                mi64 = new MediaInfoLib.MediaInfo64();
            }
            else
            {
                mi32 = new MediaInfoLib.MediaInfo32();
            }
        }
        //
        public bool getResolution(string fileName, out string videoResolution, out string audioResolution)
        {
            videoResolution = "";
            audioResolution = "";
            //
            try
            {
                if (is64)
                {
                    if (!mi64.Open(fileName)) return false;
                }
                else
                {
                    if (!mi32.Open(fileName)) return false;
                }
                //
                if (is64)
                {
                    if ("" == mi64.getAudioCodec() && "" == mi64.getVidCodec())
                    {
                        mi64.Close();
                        return false;
                    }
                    //
                    if ("" == mi64.getWidth().Trim() || "" == mi64.getHeight().Trim())
                    {
                        videoResolution = "";
                    }
                    else
                    {
                        videoResolution = mi64.getWidth() + "x" + mi64.getHeight();
                    }
                    //
                    string ac = mi64.getAudioCodec().ToUpper().Trim();
                    ac = firstPart(ac);
                    ac=audioCodecNameCnv(ac);
                    string channels = "2" == mi64.getNoChannels() ? "" : "_" + mi64.getNoChannels();
                    audioResolution = string.Format("{0}{1}{2}", divideBy1000(mi64.getAudioBitrate()), ac, channels);
                }
                else
                {
                    if ("" == mi32.getAudioCodec() && "" == mi32.getVidCodec())
                    {
                        mi32.Close();
                        return false;
                    }
                    //
                    if ("" == mi32.getWidth().Trim() || "" == mi32.getHeight().Trim())
                    {
                        videoResolution = "";
                    }
                    else
                    {
                        videoResolution = mi32.getWidth() + "x" + mi32.getHeight();
                    }
                    //
                    string ac = mi32.getAudioCodec().ToUpper().Trim();
                    ac = firstPart(ac);
                    ac=audioCodecNameCnv(ac);
                    string channels = "2" == mi32.getNoChannels() ? "" : "_" + mi32.getNoChannels();
                    audioResolution = string.Format("{0}{1}{2}", divideBy1000(mi32.getAudioBitrate()), ac, channels);
                }
                //
                if (is64) { mi64.Close(); }
                else { mi32.Close(); }
                
            }
            catch
            {
                return false;
            }
            //
            return true;
        }
        //
        private string firstPart(string s)
        {
            string []ss = s.Split(new [] {'/'}, StringSplitOptions.RemoveEmptyEntries);
            if(ss.Length>0)return ss[0];
            //
            return s;
        }
        //
        private string audioCodecNameCnv(string ac)
        {
            ac = ac.Replace("161", "WMA");
            ac = ac.Replace("A_AAC", "AAC");
            ac = ac.Replace("MPA1L1", "MP1");
            ac = ac.Replace("MPA1L2", "MP2");
            ac = ac.Replace("MPA1L3", "MP3");
            ac = ac.Replace("MPA2L3", "MP3");
            //
            return ac;
        }
        //
        private string divideBy1000(string s)
        {
            long l;
            if (Int64.TryParse(s, out l))
                return (l / 1000).ToString();
            {
            }
            return s;
        }
        //
    }

}
